<template>
    <div>
        <h1 style="text-align: center; color: rgb(13, 99, 197);">欢迎登录</h1>
    </div>
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field
                v-model="username"
                name="username"
                label="用户名"
                placeholder="用户名"
                :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
                v-model="password"
                type="password"
                name="password"
                label="密码"
                placeholder="密码"
                :rules="[{ required: true, message: '请填写密码' }]"
            />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit">提交</van-button>
        </div>
    </van-form>
</template>
<script setup lang="ts">
import { ref,onBeforeUnmount } from 'vue'
import axios from "axios";
import { useRouter } from 'vue-router'
import { Notify } from 'vant';
// 引入公共js文件
import utils from "@/utils/utils";
// 设置cookie
// utils.setCookie("index",1);
// console.log(utils);

// 获取cookie
// utils.getCookie("index");
// console.log(utils);
// 设置cookie
// utils.clearCookie("index");

const router = useRouter()
const username = ref('');
const password = ref('');
const onSubmit = (values: any) => {
    // console.log('submit', values);
    const params = {
        "admin": values.username,
        "password": values.password
    }
    axios.get('http://localhost:8080/api/login', { params }).then(
        (response: any) => {
            console.log(response.data);
            console.log(response.data.code);
            console.log(response.data.Token);
            if (response.data.code === 0) {
                
                    localStorage.setItem('user', username.value)
                    localStorage.setItem('pwd', password.value)
                    localStorage.setItem('token', response.data.Token)
                    utils.setCookie('user', username.value);
                    utils.setCookie('pwd', password.value);
                    utils.setCookie('token', response.data.Token);
                    router.push({ name: 'content' })
                
            } else if (response.data.code === 1) {
                Notify('用户名或密码不对请注册账号或重新登录');
                router.push({ name: 'enroll' })
            }else if(response.data.code === 3){
                Notify('该账号已经登录');
                router.push({ name: 'enroll' })
            }
        })
};

</script>
<style scoped>
.text {
    text-align: center;
    color: #ccc;
    font-size: 10px;
}
</style>